package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.ReloadCacheRequest;
import cc.rengu.igas.bomp.facade.response.ReloadCacheResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.CacheTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.CacheService;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.CacheServiceImpl;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @author zhangxuran
 * @author lizhangtao
 * 2020/08/18 新增智能路由缓存刷新
 * @version 1.0
 * @date 2020/03/28
 */
public class ReloadCacheService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        ReloadCacheRequest reloadCacheRequset = new ReloadCacheRequest();
        ConvertUtil.convertOutput(reloadCacheRequset);
        return reloadCacheRequset;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        ReloadCacheRequest reloadCacheRequset = (ReloadCacheRequest) request;
        ReloadCacheResponse reloadCacheResponse = new ReloadCacheResponse();
        /* 获取法人行标识 */
        String instId = reloadCacheRequset.getHeader().getInstId();
        /* 获取刷新缓存类型*/
        String cacheType = reloadCacheRequset.getCacheType();
        /* 缓存参数类型*/
        String paramType = reloadCacheRequset.getParamType();
        /*必填项校验*/
        rglog.info("请求信息：" + JSONObject.toJSONString(reloadCacheRequset));
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(cacheType)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 根据缓存类型调用不同方法 */
        CacheService cacheService = new CacheServiceImpl();
        if (CacheTypeEnum.TRANS_AUTH.getCacheType().equals(cacheType)) {
            /* 交易渠道交易权限刷新 */
            cacheService.reloadTransAuthInfo(instId, "");
            /* 支付通道交易权限刷新 */
            cacheService.reloadDstChannelTxn(instId, "");
            /* 同步刷新800的交易权限 */
            if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                /* 交易渠道交易权限刷新 */
                cacheService.reloadTransAuthInfo(AppParamConstant.DEFAULT_INSTID, "");
                /* 支付通道交易权限刷新 */
                cacheService.reloadDstChannelTxn(AppParamConstant.DEFAULT_INSTID, "");
            }
        } else if (CacheTypeEnum.CHANNEL_STATUS.getCacheType().equals(cacheType)) {
            /* 交易渠道信息刷新 */
            cacheService.reloadSrcChannelInfo(instId, "");
            /* 支付通道信息刷新 */
            cacheService.reloadDstChannelInfo(instId, "");
            /* 同步刷新800的交易渠道信息 */
            if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                /* 交易渠道交易权限刷新 */
                cacheService.reloadSrcChannelInfo(AppParamConstant.DEFAULT_INSTID, "");
                /* 支付通道交易权限刷新 */
                cacheService.reloadDstChannelInfo(AppParamConstant.DEFAULT_INSTID, "");
            }
        } else if (CacheTypeEnum.CARD_BIN.getCacheType().equals(cacheType)) {
            /* 卡bin刷新 */
            cacheService.reloadCardBinInfo();
        } else if (CacheTypeEnum.SYS_PARAM.getCacheType().equals(cacheType)) {
            /* 系统参数 */
            cacheService.reloadSysParamInfo(instId, paramType);
            /* 同步刷新800的系统参数 */
            if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                /* 系统参数 */
                cacheService.reloadSysParamInfo(AppParamConstant.DEFAULT_INSTID, paramType);
            }
        } else if (CacheTypeEnum.RSP_MAP.getCacheType().equals(cacheType)) {
            /* 错误码映射关系 */
            cacheService.reloadRspCodeMapInfo(instId);
            /* 同步刷新800的错误码映射关系 */
            if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                /* 错误码映射关系 */
                cacheService.reloadRspCodeMapInfo(AppParamConstant.DEFAULT_INSTID);
            }
        } else if (CacheTypeEnum.INTEL_ROUTE.getCacheType().equals(cacheType)) {
            /* 智能路由缓存刷新 */
            DubboService dubboService = new DubboServiceImpl();
            if (AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                dubboService.callDubboService("smartRouteManageService", "refreshCache", "");
            } else {
                dubboService.callDubboService("smartRouteManageService", "refreshCache", instId);
            }
        } else {
            rglog.error("缓存参数类型错误！");
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        BizResponse<ReloadCacheResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(reloadCacheResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ReloadCacheResponse reloadCacheResponse = (ReloadCacheResponse) bizResponse.getResult();
        ConvertUtil.convertInput(reloadCacheResponse);
    }
}
